package com.itxzh.logindemo.interceptor;


import com.itxzh.logindemo.utils.TokenRedisUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 *
 * @Author ldt
 * @Date 2022/5/20 15:46
 * @Version 1.0.0
 */
public class LoginTokenRedisInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("token");

        if(token == null) {
            throw new RuntimeException("请输入token");
        }

        if(!TokenRedisUtil.verify(token)) {
            throw new RuntimeException("token过期，请重新登录");
        }

        TokenRedisUtil.refresh(token);

        return true;
    }

}
